package com.example.admin.filter;

import cn.hutool.http.server.HttpServerRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@WebFilter("/*")
@Component
public class AAstaticFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String requestURI = request.getRequestURI();
        log.info("静态资源Filter拦截了");
        //避免后续的拦截器的处理flag的时候空指针异常
//        request.setAttribute("flag",false);
//        if(requestURI.endsWith("html")||requestURI.endsWith("js")||requestURI.endsWith("css")){
//            //放置信号量 setAttribute-往请求中增加数据
//            request.setAttribute("flag",true);

        //比较老练得方案
        request.setAttribute("flag", requestURI.endsWith("html") || requestURI.endsWith("js") || requestURI.endsWith("css"));
        //放行静态资源
        filterChain.doFilter(servletRequest, servletResponse);

        log.info("又回到静态资源Filter");
    }

}